package me.xiaoyan.android.tuition.utils;

import java.io.ByteArrayOutputStream;
import java.security.Security;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

/**
 * DES加密模块
 * 
 * @author majun
 * 
 */
public class ThreeDESUtil {
	private final static String hexString = "0123456789ABCDEF";

	static {
		Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
	}

	public static final String Algorithm_DES = "DES";

	public static final String Algorithm_3DES = "DESede";

	public static final String Algorithm_Blowfish = "Blowfish";

	private String Algorithm = Algorithm_3DES; // 定义
												// 加密算法,可用'DES','DESede','Blowfish'

	public ThreeDESUtil() {
	}

	public ThreeDESUtil(String algorithm) {
		this.Algorithm = algorithm;
	}

	/***
	 * 加密DESede
	 * 
	 * @param keybyte
	 * @param src
	 * @return
	 */
	public static byte[] encryptDESede(byte[] keybyte, byte[] src) {
		try {
			// 生成密钥
			SecretKey deskey = new SecretKeySpec(keybyte, Algorithm_3DES);
			// 加密
			Cipher c1 = Cipher.getInstance("DESede/CBC/PKCS5Padding");
			IvParameterSpec ivSpec = new IvParameterSpec(new byte[8]);
			c1.init(Cipher.ENCRYPT_MODE, deskey, ivSpec);
			return c1.doFinal(src);
		} catch (java.security.NoSuchAlgorithmException e1) {
			e1.printStackTrace();
		} catch (javax.crypto.NoSuchPaddingException e2) {
			e2.printStackTrace();
		} catch (java.lang.Exception e3) {
			e3.printStackTrace();
		}
		return null;

	}

	/***
	 * 解密DESede
	 * 
	 * @param keybyte
	 * @param src
	 * @return
	 */
	public static byte[] decryptDESede(byte[] keybyte, byte[] src) {
		try {
			// 生成密钥
			SecretKey deskey = new SecretKeySpec(keybyte, Algorithm_3DES);
			// 解密
			Cipher c1 = Cipher.getInstance("DESede/CBC/PKCS5Padding");
			IvParameterSpec ivSpec = new IvParameterSpec(new byte[8]);
			c1.init(Cipher.DECRYPT_MODE, deskey, ivSpec);
			return c1.doFinal(src);
		} catch (java.security.NoSuchAlgorithmException e1) {
			e1.printStackTrace();
		} catch (javax.crypto.NoSuchPaddingException e2) {
			e2.printStackTrace();
		} catch (java.lang.Exception e3) {
			e3.printStackTrace();
		}
		return null;
	}

	public String getAlgorithm() {
		return Algorithm;
	}

	public void setAlgorithm(String algorithm) {
		Algorithm = algorithm;
	}

	/**
	 * 将字符串编码成16进制数字
	 * 
	 * @param str
	 * @return
	 */
	public static String string2Hex(byte[] bytes) {
		StringBuilder sb = new StringBuilder(bytes.length * 2);
		// 将字节数组中每个字节拆解成2位16进制整数
		for (int i = 0; i < bytes.length; i++) {
			sb.append(hexString.charAt((bytes[i] & 0xf0) >> 4));
			sb.append(hexString.charAt((bytes[i] & 0x0f) >> 0));
		}
		return sb.toString();
	}

	/**
	 * 将16进制数字解码成字符串
	 * 
	 * @param bytes
	 * @return
	 */
	public static String hex2String(String bytes) {
		ByteArrayOutputStream baos = new ByteArrayOutputStream(
				bytes.length() / 2);
		// 将每2位16进制整数组装成一个字节
		for (int i = 0; i < bytes.length(); i += 2)
			baos.write((hexString.indexOf(bytes.charAt(i)) << 4 | hexString
					.indexOf(bytes.charAt(i + 1))));
		return new String(baos.toByteArray());
	}
}
